package com.song.studylineserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.song.studylineserver.domain.Role;
import com.song.studylineserver.domain.User;
import com.song.studylineserver.service.RoleService;
import com.song.studylineserver.mapper.RoleMapper;
import com.song.studylineserver.util.SpringUtils;
import com.song.studylineserver.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author Swtao
* @description 针对表【sl_role(角色信息表)】的数据库操作Service实现
* @createDate 2022-01-15 18:34:06
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Autowired
    RoleMapper roleMapper;


    @Override
    public List<String> selectRolePermissionByUserId(Long userId) {
        List<Role> perms=roleMapper.selectRolePermissionByUserId(userId);
        List<String> permsSet =new ArrayList<>();
        for (Role perm:perms)
        {
            if(StringUtils.isNotNull(perm))
            {

                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }

        }
        return permsSet;

    }

    /**
     * 查询所有角色
     *
     * @return 角色列表
     */
    @Override
    public List<Role> selectRoleAll()
    {
        return SpringUtils.getAopProxy(this).selectRoleList(new Role());
    }

    /**
     * 根据条件分页查询角色数据
     *
     * @param role 角色信息
     * @return 角色数据集合信息
     */
    @Override
    public List<Role> selectRoleList(Role role)
    {
        return roleMapper.selectRoleList(role);
    }

    /**
     * 根据用户ID获取角色选择框列表
     *
     * @param userId 用户ID
     * @return 选中角色ID列表
     */
    @Override
    public List<Long> selectRoleListByUserId(Long userId)
    {
        return roleMapper.selectRoleListByUserId(userId);
    }
}




